//
//  CNPayViewController.m
//  BeeQuick_19
//
//  Created by 赵玉忠 on 16/9/26.
//  Copyright © 2016年 CN Team. All rights reserved.
//

#import "CNPayViewController.h"
#import "UIBarButtonItem+CNAddNewAddressUIBarButtonItem.h"
#import "CNOrderDetailTableViewController.h"
#import "CNPayTypeModel.h"
@interface CNPayViewController ()<UIWebViewDelegate>
@property (nonatomic,copy) NSString *urlPath;

@end

@implementation CNPayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self notice];
    
    [self loadWebView];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"支付";

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"v2_goback"] forState:UIControlStateNormal];
    // 可以设置成图像的大小
    //当设置了按钮的image属性之后, 那么currentImage就会有值
    button.size = button.currentImage.size;
    [button addTarget:self action:@selector(backClick) forControlEvents: UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem =  [[UIBarButtonItem alloc] initWithCustomView:button];
    
//    [self.navigationItem setValue:button forKey:@"backBarButtonItem"];
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem loadNewUIBarButtonItemWithButtonName:@"完成" withUIColor:[UIColor darkGrayColor] andWithTarget:self andAction:@selector(saveButton)];
}

-(void)backClick{
    CNOrderDetailTableViewController *vc = [CNOrderDetailTableViewController new];
    [self.navigationController pushViewController:vc animated:YES];
}


#pragma mark - 返回主购物车界面，支付完成
-(void)saveButton{
        UINavigationController *nv = self.navigationController;
        NSInteger cout = self.navigationController.viewControllers.count;
        for (int i=0; i<2; i++) {
            UIViewController *vc = nv.viewControllers[cout-1-i];
            [vc.navigationController popViewControllerAnimated:NO];
        }
    
    //清空数据库
    [[SQLiteManager sharedSQLiteManager]eraseTable];
}

-(void)loadWebView{
    UIWebView *webView = [[UIWebView alloc]init];
    [self.view addSubview:webView];
    [webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.right.equalTo(self.view);
    }];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.urlPath]];
    [webView loadRequest:request];
    webView.delegate = self;
}

- (void) webViewDidStartLoad:(UIWebView *)webView
{
    [SVProgressHUD show];
}
- (void) webViewDidFinishLoad:(UIWebView *)webView
{
    [SVProgressHUD dismiss];
}
- (void) webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [SVProgressHUD showErrorWithStatus:@"加载失败,请查看是否联网"];
}


//设置加载路径
-(void)notice{
    NSInteger number = 0;
    for (NSInteger i=0; i<self.payTypeModelArray.count; i++) {
        CNPayTypeModel *model = self.payTypeModelArray[i];
        if ([model.RightImageIcon isEqualToString:@"v2_selected"]) {
            number = i;
        }
    }
    if (number == 1) {
        //支付宝
        self.urlPath =@"https://auth.alipay.com/login/index.htm";
    }else if (number == 3){
        //QQ
        self.urlPath = @"http://openapi.qzone.qq.com/oauth/qzoneoauth_authorize?oauth_consumer_key=222011&oauth_token=16813877498509811490&oauth_callback=http%3A%2F%2Fwww.paidai.com%2Fuser%2Foauth_qq.php%3Fstep%3D4%26secret%3DAGGxuTbnVsNBHetk";
    }else if (number == 0){
        //微信
        self.urlPath = @"https://pay.weixin.qq.com/index.php/core/home/login?return_url=%2F";
    }else{
        //快钱
        self.urlPath = @"https://www.xinhehui.com/login";
    }
}

@end
